﻿using SanXiao.Game.Common;
using SanXiao.Game.GameCenter;
using SanXiao.Game.GameCenter.Roles;
using SanXiao.Game.View.ViewEx;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SanXiao.Game.View
{
    public class PlayMap : Control
    {
        public const int ROWS = 11;
        public const int COLUMNS = 11;
        private readonly GameControlCenter _gameControlCenter;
        private readonly ImageObject _back;
        private readonly System.Timers.Timer _timer;
        private readonly RoleObject _role;
        private readonly ImageObject[] _backBlock;
        public PlayMap()
        {

            SetStyle(
           ControlStyles.ResizeRedraw
           | ControlStyles.DoubleBuffer
           | ControlStyles.UserPaint
           | ControlStyles.AllPaintingInWmPaint
           | ControlStyles.SupportsTransparentBackColor,
           true
           );

            this.MouseDown += PlayMap_MouseDown;
            _gameControlCenter = new GameControlCenter(ROWS, COLUMNS, _cellWidthDefault, _cellHeightDefault);
            _back = new ImageObject(Properties.Resources.background2);
            _role = RoleCenter.CreateRole();
            _role.Width = 222;
            _role.Height = 300;
            _role.X = 900 - 222 >> 1;
            _role.Y = 30;

            //Bitmap[] bitmaps = { Properties.Resources.txlb_1, Properties.Resources.txlb_2, Properties.Resources.txlb_3, Properties.Resources.txlb_4, Properties.Resources.txlb_5, Properties.Resources.txlb_6, Properties.Resources.txlb_7, Properties.Resources.txlb_8 };
            //_backBlock = RoleObject.ToImageObjects(bitmaps);

            _timer = new System.Timers.Timer();
            _timer.Elapsed += _timer_Elapsed;
            _timer.Interval = 100;
        }

        private void PlayMap_MouseDown(object? sender, MouseEventArgs e)
        {
            _gameControlCenter.MouseDown(sender, e);
        }

        public void Start()
        {
            _gameControlCenter.Srart();
            _timer.Start();
        }
        bool _status = false;
        private void _timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            if (_status) return;
            lock (this)
            {
                _status = true;
                _gameControlCenter.Logic();
                this.Invalidate();
                _status = false;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.Smooth();

            IntPtr hdc = e.Graphics.GetHdc();
            DrawBack(hdc);
            e.Graphics.ReleaseHdc(hdc);
            _role.Draw(g);
            DrawBlockArea(g);

            _gameControlCenter.Draw(g,this.Font);
            g.Smooth(false);
        }
        private void DrawBack(IntPtr hdc)
        {
            if (_back != null)
            {
                GDI32.DrawImage2(hdc, _back.BitmapPtr, 0, 0, _back.Width, _back.Height, this.Width, this.Height);
            }
        }
        Brush _rectB = new SolidBrush(Color.FromArgb(150, 0, 0, 0));
        private int _index = 0;
        private void DrawBlockArea(Graphics g)
        {
            //g.FillRectangle(_rectB, _rect);
            if (_backBlock != null)
            {
                IntPtr hdc = g.GetHdc();
                var back = _backBlock[_index];
                if (++_index >= _backBlock.Length)
                    _index = 0;

                GDI32.DrawImageTransparenc(hdc, back.BitmapPtr, _rect.X, _rect.Y, back.Width, back.Height, _rect.Width, _rect.Height);
                g.ReleaseHdc(hdc);
            }
        }
        const int defaultW = 900;
        const int defaultH = 900;

        const int _cellWidthDefault = 50;
        const int _cellHeightDefault = 50;

        private int _startX = 0;
        private int _startY = 0;
        private int _cellWidth = _cellWidthDefault;
        private int _cellHeight = _cellHeightDefault;
        private float _zoomX = 1f;
        private float _zoomY = 1f;
        private int blackW = 500;
        private int blackH = 500;
        private Rectangle _rect;
        protected override void OnSizeChanged(EventArgs e)
        {
            _zoomX = (float)this.Width / defaultW;
            _zoomY = (float)this.Height / defaultH;

            _cellWidth = (int)(_cellWidthDefault * _zoomX);
            _cellHeight = (int)(_cellHeightDefault * _zoomY);
            blackW = _cellWidth * COLUMNS;
            blackH = _cellHeight * ROWS;
            _startX = this.Width - blackW >> 1;
            //_startY = this.Height - _cellHeight * ROWS >> 1;
            _startY = (int)(340 * _zoomY);
            _rect = new Rectangle(_startX, _startY, blackW, blackH);
            _gameControlCenter.SizeChange(ROWS, COLUMNS, _startX, _startY, _cellWidth, _cellHeight, _zoomX, _zoomY);

            this.Refresh();
        }
        protected override void Dispose(bool disposing)
        {
            _timer.Stop();
            _timer.Dispose();
            base.Dispose(disposing);
            _rectB.Dispose();
            _gameControlCenter.Dispose();
            if (_backBlock != null)
            {
                foreach (var item in _backBlock)
                {
                    item.Dispose();
                }
            }
        }
    }
}
